package duyd.ws.training;

import java.util.Date;
import java.util.Set;

import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.web.client.RestTemplate;

import duyd.ws.training.domain.Contact;
import duyd.ws.training.domain.ContactTelDetail;
import duyd.ws.training.domain.Contacts;
import duyd.ws.training.domain.Hobby;

public class RestfulClientSample {

	private static final String URL_GET_ALL_CONTACT = "http://localhost:8080/training/restful/contact/list";

	private static final String URL_GET_CONTACT_BY_ID = "http://localhost:8080/training/restful/contact/{id}";

	private static final String URL_CREATE_CONTACT = "http://localhost:8080/training/restful/contact/";

	private static final String URL_UPDATE_CONTACT = "http://localhost:8080/training/restful/contact/{id}";

	private static final String URL_DELETE_CONTACT = "http://localhost:8080/training/restful/contact/{id}";

	private static final String URL_GET_ALL_CONTACT_WITH_DETAIL = "http://localhost:8080/training/restful/contact/detail";

	public static void main(String[] args) {

		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:restful-client-app-context.xml");
		ctx.refresh();

		Contacts contacts;
		Contact contact;

		RestTemplate restTemplate = ctx.getBean("restTemplate",
				RestTemplate.class);

		System.out.println("\n ===== List all contacts: ");
		contacts = restTemplate.getForObject(URL_GET_ALL_CONTACT,
				Contacts.class);
		listContacts(contacts);

		System.out.println("\n ===== Retrieve one contact by id: ");
		contact = restTemplate.getForObject(URL_GET_CONTACT_BY_ID,
				Contact.class, 1);
		System.out.println(contact);

		System.out.println("\n ===== Update a contact: ");
		contact.setBirthDate(new Date());
		restTemplate.put(URL_UPDATE_CONTACT, contact, 1);
		System.out.println("Contact updated successfully: " + contact);

		System.out.println("\n ===== Insert a contact: ");
		contact = new Contact();
		contact.setBirthDate(new Date());
		contact.setFirstName("Web");
		contact.setLastName("Service");
		restTemplate.postForObject(URL_CREATE_CONTACT, contact, Contact.class);
		System.out.println("Contact inserted successfully: " + contact);
		System.out.println("\n ===== List all contacts: ");
		contacts = restTemplate.getForObject(URL_GET_ALL_CONTACT,
				Contacts.class);
		listContacts(contacts);

		System.out.println("\n ===== Delete a contact: ");
		restTemplate.delete(URL_DELETE_CONTACT, 4);
		System.out.println("Contact deleted successfully: " + contact);
		System.out.println("\n ===== List all contacts: ");
		contacts = restTemplate.getForObject(URL_GET_ALL_CONTACT,
				Contacts.class);
		listContacts(contacts);

		contacts = restTemplate.getForObject(URL_GET_ALL_CONTACT_WITH_DETAIL,
				Contacts.class);
		listContactsWithDetail(contacts);

		System.out.println("\n ===== Insert an invalid contact: ");
		contact = new Contact();
		contact.setBirthDate(new Date());
		contact.setFirstName("In");
		contact.setLastName("Valid");
		restTemplate.postForObject(URL_CREATE_CONTACT, contact, Contact.class);
		System.out.println("Contact inserted successfully: " + contact);
		System.out.println("\n ===== List all contacts: ");
		contacts = restTemplate.getForObject(URL_GET_ALL_CONTACT,
				Contacts.class);
		listContacts(contacts);

	}

	private static void listContacts(Contacts contacts) {
		for (Contact contact : contacts.getContacts()) {
			System.out.println(contact);
		}
	}

	public static void listContactsWithDetail(Contacts contacts) {
		for (Contact contact : contacts.getContacts()) {
			System.out.println(contact);
			Set<ContactTelDetail> tels = contact.getTels();
			for (ContactTelDetail tel : tels) {
				System.out.println("\t" + tel);
			}
			Set<Hobby> hobbies = contact.getHobbies();
			for (Hobby hobby : hobbies) {
				System.out.println("\t" + hobby);
			}
		}
	}
}
